package com.jushu.storbox;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jushu.storbox.adapter.WelcomeViewPageAdapter;
import com.jushu.storbox.fragment.WelcomePageA;
import com.jushu.storbox.fragment.WelcomePageB;
import com.jushu.storbox.fragment.WelcomePagec;
import com.jushu.storbox.manager.ActivityTaskManager;

public class WelcomeActivity extends FragmentActivity {
	private ViewPager viewPager;
	private ArrayList<Fragment> fragmentList = null;
	private String LOG_TAG = "WelcomeActivity";
	private LinearLayout pageLayout;
	private WelcomePageA wPageA;
	private WelcomePageB wPageB;
	private WelcomePagec wePageC;
	private int pageID = 0;
	private boolean isWelcomeButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		ActivityTaskManager.getInstance().putActivity(LOG_TAG,
				WelcomeActivity.this);
		setContentView(R.layout.welcome_layout);
		initView();
		initData();
	}

	private void initView() {
		viewPager = (ViewPager) findViewById(R.id.welcome_viewpage);
		viewPager.setOnPageChangeListener(new PageChangeListener());
		pageLayout = (LinearLayout) findViewById(R.id.welcome_page_layout);
	}

	private class PageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			pageLayout.getChildAt(arg0).setBackgroundResource(
					R.mipmap.welcome_page_b);
			pageLayout.getChildAt(pageID).setBackgroundResource(
					R.mipmap.welcome_page_a);
			pageID = arg0;
		}
	}

	private void initData() {
		isWelcomeButton=getIntent().getBooleanExtra("isWelcomeButton", false);
		fragmentList = new ArrayList<Fragment>();
		wPageA = new WelcomePageA();
		fragmentList.add(wPageA);
		wPageB = new WelcomePageB();
		fragmentList.add(wPageB);
		wePageC = new WelcomePagec(isWelcomeButton);
		fragmentList.add(wePageC);
		viewPager.setAdapter(new WelcomeViewPageAdapter(WelcomeActivity.this
				.getSupportFragmentManager(), fragmentList));
		initPageView();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ActivityTaskManager.getInstance().removeActivity(LOG_TAG);
	}

	private void initPageView() {
		for (int i = 0; i < fragmentList.size(); i++) {
			ImageView page = new ImageView(getApplicationContext());
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(8, 0, 8, 0);
			page.setLayoutParams(layoutParams);
			if (i == 0) {
				page.setBackgroundResource(R.mipmap.welcome_page_b);
			} else {
				page.setBackgroundResource(R.mipmap.welcome_page_a);
			}
			pageLayout.addView(page);
		}
	}
}
